package com.niit.order;

import com.niit.feign.clients.UserClient;
import com.niit.feign.config.UserClientConfig;
import com.niit.feign.fallback.UserClientFallback;
import com.niit.order.myImpl.MyUserClientFallback;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.web.client.RestTemplate;

/**
 * @author yaojianfeng
 */
@MapperScan("com.niit.order.mapper")
@SpringBootApplication
@EnableCircuitBreaker //启用断路器功能
@ServletComponentScan(basePackages = {"com.niit.order.filter"})
@EnableFeignClients(clients = {UserClient.class},defaultConfiguration = UserClientConfig.class) //解决包扫描无法注入问题
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

    /**
     * 在容器中注册 RestTemplate 方便项目内调用
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }


    /**
     * 全局的负载均衡规则
     * @return
     */
//    @Bean
//    public IRule rule(){
//        return new RandomRule();
//    }

    /**
     * 在容器中注入UserClient的统一服务降级方法
     * 	可以注入order-service自定义的 MyUserClientFallback服务降级类
     * @return
     */
    @Bean
    public UserClientFallback userClientFallback(){
        return new MyUserClientFallback();
    }


}